home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / GSRC208A.ZIP / GEPMAIN.C < prev    next >
C/C++ Source or Header  |  1993-08-24  |  13KB  |  458 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*         main control block        */
  11. /*                                   */
  12. /*        Zortech C/C++ 3.0 r4       */
  13. /*          MICROSOFT C 6.00         */
  14. /*          Visual C/C++ 1.0         */
  15. /*           QuickC/WIN 1.0          */
  16. /*             ULTRIX cc             */
  17. /*              GNU gcc              */
  18. /*                                   */
  19. /*   (include here compilers that    */
  20. /*   compiled GEPASI successfully)   */
  21. /*                                   */
  22. /*************************************/
  23.  
  24.  
  25. #ifndef MSDOS
  26. #define COMMON
  27. #endif
  28.  
  29. #ifdef _WINDOWS
  30. #define COMMON
  31. #endif
  32.  
  33. #if (_MSC_VER >= 610)                           /* QuickC/WIN or C/C++ 7.0                              */
  34.  #define fcloseall _fcloseall
  35.  #define unlink _unlink
  36.  #define putenv _putenv
  37. #endif
  38. #if ((_MSC_VER == 600)||(_MSC_VER==700))        /* MICROSOFT C 6.00 & 7.00              */
  39.  #pragma comment( exestr, "Compiled on " __DATE__ " at " __TIME__ )
  40. #endif
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <time.h>
  45. #include <math.h>
  46. #include <float.h>
  47. #include <setjmp.h>
  48. #include <signal.h>
  49. #include <stdlib.h>
  50. /*#include <conio.h>*/
  51.  
  52. #ifdef _WINDOWS
  53. #include <io.h>
  54. #include <fcntl.h>
  55. #endif
  56.  
  57. #include "pmu.h"                                    /* several utilities     */
  58. #include "globals.h"                                /* global symbols        */
  59. #include "globvar.h"                                /* global variables      */
  60.  
  61. #ifndef _WINDOWS
  62. #include "except.c"                                 /* exception error handl.*/
  63. #endif
  64.  
  65. #include "r250.h"                                                                       /* pseudo-random numbers */
  66. #include "matrix.h"                                 /* matrix algebra        */
  67. #include "kinetics.h"                               /* rate eq. & deriv.     */
  68. #include "rates.h"                                  /* net rates of met.     */
  69. #include "gauss.h"                                  /* structural properties */
  70. #include "newton.h"                                 /* steady state          */
  71. #include "metconan.h"                               /* metabolic control     */
  72. #include "lsoda.h"
  73. #include "dynamic.h"                                    /* transient behaviour   */
  74. #include "datab.h"
  75. #include "getall.h"
  76. #include "gepout.h"                                 /* data output           */
  77. #include "heapchk.h"
  78.  
  79. #ifdef _ZTC
  80. #define MEM_DEBUG 1
  81. #include "mem.h"
  82. #else
  83. #define mem_malloc malloc
  84. #define mem_free free
  85. #define mem_realloc realloc
  86. #endif
  87.  
  88. /* tidy up memory and disk                                                      */
  89.  
  90. void tidy( void )
  91. {
  92.  /* if -d switch delete the file with list of simulations       */
  93.  if( delete ) unlink( listfile );
  94.  
  95.  /* free up all the dynamic memory blocks                                       */
  96.  FreeMem();
  97. }
  98.  
  99. /* terminate gepasi due to errors */
  100.  
  101. void term_gepasi( void )
  102. {
  103.  tidy();
  104.  fprintf(stderr,errormsg[2]);
  105. #ifdef _MSC_VER
  106.  fcloseall();
  107. #endif
  108. #ifdef _ZTC
  109.  mem_term();
  110. #endif
  111.  exit( 1 );                                         /* signal error          */
  112. }
  113.  
  114. /* resets the state of the internal metabolites         */
  115.  
  116. void reset_state( double *keep )
  117. {
  118.  int i;
  119.  
  120.  for( i=0; i<indmet; i++ ) x[i] = x0[i];
  121.  if( &endtime != keep ) endtime = options.endtime;
  122. }
  123.  
  124. /* sets the values of the linked variables                      */
  125.  
  126. void set_linked( void )
  127. {
  128.  int i;
  129.  
  130.  for( i=0; i<nlinks; i++)
  131.   switch( sparam[lindex[i]].operation )
  132.   {
  133.    case 1: *(sparam[lindex[i]].var) = sparam[lindex[i]].factor *
  134.                       *(sparam[lindex[i]].linkedto);
  135.        break;
  136.   }
  137. }
  138.  
  139.  
  140. /* main simulation procedure                                    */
  141.  
  142. int simulate( int p, int simple )
  143. {
  144.  int i, ccfs, diverge;
  145.  time_t itime, ftime;
  146.  
  147. #ifndef _WINDOWS
  148.   /* Save stack environment for return in case of error.                */
  149.   /*  First time through, jmpret is 0, so true conditional              */
  150.   /*  is executed. If an error occurs, jmpret will be set               */
  151.   /*  to -1 and false conditional will be executed.                             */
  152.   jmpret = setjmp( mark );
  153.   if( jmpret == 0 )
  154.   {
  155. #endif
  156.  
  157.   /* reset concentration of external metabolites*/
  158.   for( i=indmet; i<totmet; i++ ) x[i] = x0[i];
  159.  
  160.   /* set the values of the linked parameters    */
  161.   if( nlinks ) set_linked();
  162.  
  163.   /* initialize the conserved moiety concentrations                                             */
  164.   init_moiety();
  165.   /* print the first part of the report file    */
  166.   if( options.txt ) report_init( p );
  167.  
  168.   /* dynamic simulation */
  169.   if( options.dyn )
  170.   {
  171.    time(&itime);
  172.    dynamic();                           /* numeric integration */
  173.    time(&ftime);
  174.    ftime -= itime;
  175.  
  176.    if ( ! options.debug ) set_cursor(cx, cy);
  177.  
  178.    if( simple )
  179.     printf( "\n  - integration completed in %ld s, ", ftime );
  180.    else printf( "." );
  181.  
  182.    /* calculate the fluxes      */
  183.    calcfluxes( x );
  184.  
  185.    /* print integration results in report file  */
  186.    if( options.txt ) report_dyn();
  187.    if( options.dat  && (!options.datss) ) dat_values();
  188.   }
  189.   else
  190.    for( i=0; i<nmetab; i++) x[i] = 10 * options.hrcz;
  191.  
  192.   /* steady-state solution      */
  193.   if ( options.ss )
  194.   {
  195.    if( simple )
  196.     if( ! options.debug ) printf( "\n  - ss solution " );
  197.    time( &itime );
  198.    if( (diverge = newton()) )                               /* steady state                 */
  199.    {
  200.     endtime = 1e+10;                                        /* new endtime                  */
  201.     int_to_ss();                                            /* new numeric integr.          */
  202.     diverge = newton();                                        /* try again st. state            */
  203.    }
  204.    time(&ftime);
  205.    ftime -= itime;
  206.  
  207.    if ( options.debug ) printf( "\n\n  - ss solution in %ld s\n", ftime );
  208.    else
  209.     if( simple ) printf( "in %ld s", ftime );
  210.     else printf( "." );
  211.  
  212.    if ( diverge == N_OK )
  213.    {
  214.     calcfluxes( xss );
  215.     calc_tt();                                        /* calc. trans. times  */
  216.     calc_Dxv();                                       /* calc. elasticities  */
  217.     if ( ( ccfs = calc_Gamma() ) != MCA_NOCC )        /* calc. [] cnt.coeff. */
  218.      calc_C();                                        /* calc. J cnt.coeff.  */
  219.    }
  220.  
  221.    if( options.txt ) report_ss( diverge, ccfs );
  222.  
  223.    /* scale the control coefficients if necessary       */
  224.    if( (options.dat) && (!options.datmca ) ) scale_coef( );
  225.    /* copy xss to x                                                                     */
  226.    memcpy( (void *) x, (void *) xss, sizeof( x ) );
  227.  
  228.    /* output data in columnar file      */
  229.    if( options.dat && options.datss && (diverge==N_OK) ) dat_values();
  230.   }
  231.  
  232.   if( options.txt && options.debug ) report_tech();
  233.   return 0;
  234. #ifndef _WINDOWS
  235.   }
  236.   else
  237.   {
  238.    fpcheck();
  239.    if( options.txt ) report_error();
  240.    if( options.dat ) dat_error();
  241.   }
  242. #endif
  243. }
  244.  
  245. int hypergrid( int index, int p )
  246. {
  247.  int i;
  248.  
  249.  *(sparam[scindex[index]].var) = sparam[scindex[index]].low;
  250.  if( options.dyn ) reset_state(sparam[scindex[index]].var);
  251.  for( i=0; i<sparam[scindex[index]].dens+1; i++ )
  252.  {
  253.   if( index != 0 )
  254.   {
  255.    hypergrid( index-1, p );
  256.   }
  257.   else
  258.   {
  259.    printf("\n %lu ", ++iter );
  260.    simulate( p, 0 );
  261.   }
  262.   if( sparam[scindex[index]].log )
  263.    *(sparam[scindex[index]].var) = sparam[scindex[index]].low *
  264.                    pow( 10, sparam[scindex[index]].ampl /
  265.                         sparam[scindex[index]].dens * (i+1)
  266.                       );
  267.   else
  268.    *(sparam[scindex[index]].var) = sparam[scindex[index]].low +
  269.                    (i+1) *
  270.                    sparam[scindex[index]].ampl /
  271.                    sparam[scindex[index]].dens;
  272.   if( options.dyn ) reset_state(sparam[scindex[index]].var);
  273.   if( (totscan > 1) && (index == totscan - 1) ) dat_nl();
  274.  }
  275.  return 0;
  276. }
  277.  
  278. int randomfill( int p )
  279. {
  280.  unsigned long i;
  281.  int j;
  282.  
  283.  fo